<template>
  <div class="home">
    <!-- 顶部Banner -->
    <div class="hero-section">
      <div class="hero-overlay"></div>
      <div class="hero-content">
        <h1 class="animate__animated animate__fadeInDown">
          让每个孩子都能享有健康快乐的心灵
        </h1>
        <p class="animate__animated animate__fadeInUp animate__delay-1s">
          专业的心理健康服务平台，为留守儿童提供全方位的心理关怀
        </p>
        <div class="hero-buttons animate__animated animate__fadeInUp animate__delay-2s">
          <el-button type="primary" size="large" @click="router.push('/consultation')">
            开始咨询
          </el-button>
          <el-button size="large" @click="router.push('/evaluation')">
            心理测评
          </el-button>
        </div>
      </div>
      <div class="wave-container">
        <svg class="wave" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1440 320">
          <path fill="#f5f7fa" fill-opacity="1" d="M0,96L48,112C96,128,192,160,288,160C384,160,480,128,576,112C672,96,768,96,864,112C960,128,1056,160,1152,160C1248,160,1344,128,1392,112L1440,96L1440,320L1392,320C1344,320,1248,320,1152,320C1056,320,960,320,864,320C768,320,672,320,576,320C480,320,384,320,288,320C192,320,96,320,48,320L0,320Z"></path>
        </svg>
      </div>
    </div>

    <!-- 主要功能入口 -->
    <div class="features-section">
      <el-row :gutter="30">
        <el-col :span="6" v-for="(feature, index) in features" :key="index">
          <div class="feature-card" @click="router.push(feature.path)">
            <el-icon :size="50" :class="feature.iconClass">
              <component :is="feature.icon" />
            </el-icon>
            <h3>{{ feature.title }}</h3>
            <p>{{ feature.description }}</p>
          </div>
        </el-col>
      </el-row>
    </div>

    <!-- 温馨社区 -->
    <div class="section">
      <div class="section-header">
        <h2>温馨社区</h2>
        <el-button type="primary" link @click="router.push('/community/list')">
          查看更多
        </el-button>
      </div>
      
      <div class="articles">
        <el-card 
          v-for="article in latestArticles" 
          :key="article.id" 
          class="article-card"
          @click="viewArticle(article)"
        >
          <div class="article-header">
            <div class="user-info">
              <el-avatar :size="40" :src="article.authorAvatar" />
              <div class="author-info">
                <div class="author-name">{{ article.authorName }}</div>
                <div class="publish-time">{{ formatDateTime(article.createdAt) }}</div>
              </div>
            </div>
          </div>

          <div class="article-content">
            <h3 class="article-title">{{ article.title }}</h3>
            <div class="article-preview" v-if="article.content">
              {{ article.content.slice(0, 100) }}...
            </div>
            <el-image 
              v-if="article.coverImage"
              :src="getImageUrl(article.coverImage)"
              class="cover-image"
              fit="cover"
            />
          </div>

          <div class="article-footer">
            <div class="article-stats">
              <span class="stat-item">
                <el-icon><View /></el-icon>
                {{ article.viewCount }}
              </span>
              <span class="stat-item">
                <el-icon><ChatDotRound /></el-icon>
                {{ article.commentCount }}
              </span>
              <span class="stat-item">
                <el-icon><Star /></el-icon>
                {{ article.likeCount }}
              </span>
            </div>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {
  ChatDotRound,
  DocumentChecked,
  Reading,
  UserFilled,
  View,
  Star
} from '@element-plus/icons-vue'
import request from '@/utils/request'

const router = useRouter()
const latestArticles = ref([])

// 主要功能入口
const features = [
  {
    title: '心理测评',
    description: '专业的心理健康评估，帮助了解自己',
    icon: 'DocumentChecked',
    iconClass: 'icon-assessment',
    path: '/evaluation'
  },
  {
    title: '在线咨询',
    description: '一对一专业咨询，解决心理困扰',
    icon: 'ChatDotRound',
    iconClass: 'icon-consultation',
    path: '/consultation'
  },
  {
    title: '心理课堂',
    description: '丰富的心理健康教育资源',
    icon: 'Reading',
    iconClass: 'icon-education',
    path: '/resources'
  },
  {
    title: '温馨社区',
    description: '分享交流，互助成长',
    icon: 'UserFilled',
    iconClass: 'icon-community',
    path: '/community'
  }
]

// 获取最新文章
const fetchLatestArticles = async () => {
  try {
    const res = await request.get('/article/latest')
    if (res.data) {
      latestArticles.value = res.data
    }
  } catch (error) {
    console.error('获取最新文章失败:', error)
  }
}

// 查看文章详情
const viewArticle = (article) => {
  router.push(`/community/article/${article.id}`)
}

const getImageUrl = (fileName) => {
  if (!fileName) return ''
  return `${import.meta.env.VITE_API_URL}/api/file/download/${fileName}`
}

const formatDateTime = (time) => {
  if (!time) return ''
  const date = new Date(time)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false
  })
}

onMounted(() => {
  fetchLatestArticles()
})
</script>

<style scoped>
.home {
  background-color: #f5f7fa;
}

.hero-section {
  position: relative;
  height: 500px;
  background: url('../assets/images/hero-bg.jpg');
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: white;
  margin-bottom: 40px;
  overflow: hidden;
}

.hero-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, rgba(64, 158, 255, 0.8), rgba(103, 194, 58, 0.8));
  z-index: 1;
}

.hero-content {
  position: relative;
  max-width: 800px;
  padding: 0 20px;
  z-index: 2;
}

.hero-content h1 {
  font-size: 42px;
  font-weight: 600;
  margin-bottom: 24px;
  line-height: 1.4;
  text-shadow: 0 2px 4px rgba(0,0,0,0.2);
}

.hero-content p {
  font-size: 20px;
  opacity: 0.95;
  margin-bottom: 32px;
  line-height: 1.6;
  text-shadow: 0 1px 2px rgba(0,0,0,0.1);
}

.hero-buttons {
  display: flex;
  gap: 16px;
  justify-content: center;
}

.hero-buttons .el-button {
  padding: 12px 32px;
  font-size: 16px;
  border-radius: 25px;
  transition: all 0.3s ease;
}

.hero-buttons .el-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.wave-container {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 2;
}

.wave {
  display: block;
  width: 100%;
  height: 100px;
}

/* 添加动画类 */
.animate__animated {
  animation-duration: 1s;
  animation-fill-mode: both;
}

.animate__fadeInDown {
  animation-name: fadeInDown;
}

.animate__fadeInUp {
  animation-name: fadeInUp;
}

.animate__delay-1s {
  animation-delay: 0.5s;
}

.animate__delay-2s {
  animation-delay: 1s;
}

@keyframes fadeInDown {
  from {
    opacity: 0;
    transform: translate3d(0, -50%, 0);
  }
  to {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translate3d(0, 50%, 0);
  }
  to {
    opacity: 1;
    transform: translate3d(0, 0, 0);
  }
}

.features-section {
  padding: 40px 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.feature-card {
  text-align: center;
  padding: 30px;
  background: white;
  border-radius: 8px;
  transition: all 0.3s;
  cursor: pointer;
}

.feature-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.feature-card h3 {
  margin: 15px 0;
  font-size: 20px;
}

.feature-card p {
  color: #666;
}

.icon-assessment { color: #409EFF; }
.icon-consultation { color: #67C23A; }
.icon-education { color: #E6A23C; }
.icon-community { color: #F56C6C; }

.section {
  margin-bottom: 30px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 500;
}

.articles {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 20px;
}

.article-card {
  cursor: pointer;
  transition: all 0.3s;
}

.article-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.article-header {
  margin-bottom: 15px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.author-info {
  display: flex;
  flex-direction: column;
}

.author-name {
  font-size: 16px;
  font-weight: 500;
}

.publish-time {
  font-size: 12px;
  color: #999;
}

.article-content {
  margin-bottom: 15px;
}

.article-title {
  margin: 0 0 10px;
  font-size: 18px;
  font-weight: 500;
  color: #333;
}

.article-preview {
  color: #666;
  line-height: 1.5;
  margin-bottom: 10px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.cover-image {
  width: 100%;
  height: 150px;
  border-radius: 4px;
  margin-top: 10px;
}

.article-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #999;
}

.article-stats {
  display: flex;
  gap: 20px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .hero-section {
    height: 400px;
    background-attachment: scroll;
  }

  .hero-content h1 {
    font-size: 32px;
  }

  .hero-content p {
    font-size: 18px;
  }

  .hero-buttons {
    flex-direction: column;
    align-items: center;
  }

  .hero-buttons .el-button {
    width: 80%;
  }

  .features-section .el-col {
    width: 100%;
    margin-bottom: 20px;
  }
}
</style> 